/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

import com.domain.Announcement;
import com.domain.Event;
import com.domain.User;

public class EventDAO extends BaseObjectDAO{
	public Event getEventByKeyId(Long keyId){
		Event event = null;
		if((keyId!=null)&&(keyId>0)){
			Criteria crit = this.getSession().createCriteria(Event.class);
			crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
	       	crit.add(Expression.eq("keyId",keyId));
	       	crit.add(Expression.eq("deleted",false));
	       	List<Event> results = crit.list();
	       	if(results.size()>0)event = results.get(0);
	       	else return null;
		}
		return event;
	}
	
	public Event getEventByTitle(String title){
		Event event = null;
		if((title!=null)&&(title.equals(""))){
			Criteria crit = this.getSession().createCriteria(Event.class);
			crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
	       	crit.add(Expression.eq("eventTitle",title));
	       	crit.add(Expression.eq("deleted",false));
	       	List<Event> results = crit.list();
	       	event = results.get(0);
		}
		return event;
	}
	
	public Event save(Event event) {
		Timestamp now = new Timestamp(System.currentTimeMillis());
		event.setLastModDate(now);
		this.getSession().saveOrUpdate(event);
		this.getSession().flush();
		if((event.getKeyId()!=null)){
			return event;
		}
		else return null;
	}
	
	public Event saveEvent(Long keyId, User user, Timestamp startDate, Timestamp endDate, boolean reoccuringEvent, String eventTitle,
			String eventDescription, String eventLocation, String details, boolean publicViewable, boolean approved){
		Event event = null;
		if((keyId!=null)&&(keyId>0)){
			event = this.getEventByKeyId(keyId);
			if(event!=null){
				event.setStartDate(startDate);
				event.setEndDate(endDate);
				event.setReoccuringEvent(reoccuringEvent);
				event.setEventTitle(eventTitle);
				event.setEventDescription(eventDescription);
				event.setEventLocation(eventLocation);
				event.setDetails(details);
				event.setPublicViewable(publicViewable);
				event.setApproved(approved);
				event.setLastUpdateUser(user);
				event = this.save(event);
			}
		}
		else{
			event = new Event();
			Timestamp now = new Timestamp(System.currentTimeMillis());
			event.setStartDate(startDate);
			event.setEndDate(endDate);
			event.setReoccuringEvent(reoccuringEvent);
			event.setEventTitle(eventTitle);
			event.setEventDescription(eventDescription);
			event.setEventLocation(eventLocation);
			event.setDetails(details);
			event.setPublicViewable(publicViewable);
			event.setApproved(approved);
			event.setCreationDate(now);
			event.setCreatedBy(user);
			event.setLastUpdateUser(user);
			event = this.save(event);
		}
		return event;
	}
	
	public List<Event> getAllEvents(){
		Criteria crit = this.getSession().createCriteria(Event.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
       	List<Event> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

	public List<Event> getPendingEvents() {
		Criteria crit = this.getSession().createCriteria(Event.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		crit.add(Expression.eq("approved",Boolean.FALSE));
		crit.add(Expression.eq("deleted",false));
       	List<Event> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

	public List<Event> getTopEvents() {
		Criteria crit = this.getSession().createCriteria(Event.class);
		long oneDay = 86400000;
		long twoWeeks = 14*oneDay;
		Timestamp now = new Timestamp(System.currentTimeMillis());
		Timestamp then = new Timestamp(System.currentTimeMillis() + twoWeeks);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		crit.add(Expression.eq("approved",Boolean.TRUE));
		crit.add(Expression.eq("deleted",false));
		crit.add(Expression.between("startDate", now, then));
       	List<Event> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}

	public List<Event> getAllUndeletedEvents() {
		Criteria crit = this.getSession().createCriteria(Event.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		crit.add(Expression.eq("deleted",false));
       	List<Event> results = crit.list();
       	if(results.size()>0)return results;
       	else return null;
	}
}
